package com.aat.dizena.base.web.controller;

import java.util.*;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import com.aat.dxfy.utils.Atools;
import com.aat.dizena.base.web.controller.BaseController;

import com.aat.dizena.base.bean.Role;
import com.aat.dizena.base.service.RoleService;
import com.aat.dizena.base.service.RoleresService;
/**
 * @time 2016-05-25
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 角色管理
 * @info java spring aop
 * @update none
 */
@Controller
@Scope("prototype")
@RequestMapping("master/role")
public class MasterRoleController  extends BaseController{

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleresService roleresService;

    private String list = "/master/role/list";
    private String edit = "/master/role/edit";
    private String reList = "redirect:" + list;
    private String addAction = "master/role/add";
    private String uptAction = "master/role/upt";
    private String delAction = "master/role/del";
    private String StringKey = " ";
    private String addTitle = "增加"+StringKey;
    private String uptTitle = "修改"+StringKey;
    private String listHead = StringKey+"管理";

    // list get
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(Model m) {
        m.addAttribute("addAction", addAction);
        m.addAttribute("uptAction", uptAction);
        m.addAttribute("delAction", delAction);
        m.addAttribute("listHead", listHead);
        m.addAttribute("list", list);
        return list;
    }

    // list post
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "s", required = false, defaultValue = "1") int start,
            @RequestParam(value = "p", required = false, defaultValue = "10") int perPage,
            @RequestParam(value = "f", required = false, defaultValue = "id") String sortFiled,
            @RequestParam(value = "dir", required = false, defaultValue = "DESC") String direction,

        @RequestParam(value = "u", required = false) String skey) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sortFiled", sortFiled);
        params.put("direction", direction);
        params.put("start", (start - 1) * perPage);
        params.put("perpage", perPage);
        // 其他参数
        if (isNotEmpty(skey)) {
            params.put("skey", skey);
        }

        //  返回参数
        List<Role> lists = roleService.queryAll(params);
        Long count = roleService.queryCount(params);
        Long tmp = count / perPage;
        Long pages = count % perPage == 0 ? tmp : tmp + 1;
        Map<String, Object> json = new HashMap<String, Object>();
        json.put("datas", lists);// 数据
        json.put("pages", pages);// 总页
        json.put("start", start);// 页码
        json.put("count", count);// 总数
        return json;
    }

    // add get
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add(Model model) {
        model.addAttribute("action", addAction);
        model.addAttribute("title", addTitle);
        return edit;
    }

    // add post
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(
    		@RequestParam(value = "r", required = false) String role,
			@RequestParam(value = "d", required = false) String desc,
			@RequestParam(value = "s", required = false) String ressId
			,Model model) {
        model.addAttribute("action", addAction);
        model.addAttribute("title", addTitle);
        // 验证
		if (isEmpty(role)) {
			model.addAttribute("error", "角色的名称不为空");
			return edit;
		}
		if (isEmpty(ressId)) {
			model.addAttribute("error", "角色的资源不为空");
			return edit;
		}
        //end 
		roleService.addRole(role, desc, ressId);
        return reList;
    }

    // upt get
    @RequestMapping(value = "upt/{id}", method = RequestMethod.GET)
    public String upt(@PathVariable("id") String id,Model model) {
        model.addAttribute("action", uptAction);
        model.addAttribute("title", uptTitle);
        Role m = roleService.queryBean(id);
        model.addAttribute("m", m);
        List<String> ressids = roleService.queryResIds(id);
        model.addAttribute("ressids", Atools.list2Str(ressids));
		// 要查出所有的资源，然后选中已有的资源
        return edit;
    }

    // upt post
    @RequestMapping(value = "upt", method = RequestMethod.POST)
    public String upt(
    		@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "r", required = false) String role,
			@RequestParam(value = "d", required = false) String desc,
			@RequestParam(value = "s", required = false) String ressId,Model model) {
        model.addAttribute("action", uptAction);
        model.addAttribute("title", uptTitle);
        
        Role m = roleService.queryBean(id);
        model.addAttribute("m", m);
        
        List<String> ressids = roleService.queryResIds(id);
        model.addAttribute("ressids", Atools.list2Str(ressids));
        // 验证
        if (isEmpty(role)) {
			model.addAttribute("error", "角色的名称不为空");
			return edit;
		}
		if (isEmpty(ressId)) {
			model.addAttribute("error", "角色的资源不为空");
			return edit;
		}
        //end 
		roleService.updateRole(id, role, desc, ressId);
        return reList;
    }

    // del
    @RequestMapping(value = "del/{id}", method = RequestMethod.GET)
    public String del(@PathVariable("id") String id) {
        roleService.deleteBean(id);
        roleresService.deleteRole(id);
        return reList;
    }

}
